package com.fh.modules.ss.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fh.common.constant.CommonConstant;
import com.fh.common.query.QueryWrapperUtil;
import com.fh.modules.ss.entity.SsMaterialInst;
import com.fh.modules.ss.mapper.SsMaterialInstMapper;
import com.fh.modules.ss.service.ISsMaterialInstService;
import com.fh.modules.system.model.TreeModel;
import com.fh.modules.system.service.ISysDictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fh.common.web.impl.AbstractServiceImpl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Description: 物料实例
 * @author： code generator
 * @date：   2019-09-09
 * @version： V1.0
 */
@Service
public class SsMaterialInstServiceImpl extends AbstractServiceImpl<SsMaterialInstMapper, SsMaterialInst> implements ISsMaterialInstService {

    @Autowired
    private ISysDictService sysDictService;
    public List<TreeModel> convert(){
        List<TreeModel> treeModelList=new ArrayList<>();
        List<Map<String,String>> keyValue=sysDictService.queryDictItemsByCode("materialType");
        for (Map<String, String> map : keyValue) {
            String value=(String)map.get("text");
            TreeModel treeModel=new TreeModel(value,value,null,value,null,false);
            treeModel.setSelectable(false);
            List<SsMaterialInst> ssMaterialInstList=this.queryByTypeName(value);
            if(ssMaterialInstList.size()>0){
                treeModel.setChildren(this.convert(ssMaterialInstList,value));
            }
            treeModelList.add(treeModel);
        }

        return treeModelList;
    }

    public List<TreeModel> convert(List<SsMaterialInst> ssMaterialInstList,String parentId){
        List<TreeModel> children=new ArrayList<>();
        for (SsMaterialInst ssMaterialInst : ssMaterialInstList) {
            TreeModel treeModel=new TreeModel(ssMaterialInst.getId(),ssMaterialInst.getName(),parentId,ssMaterialInst.getName(),null,true);
            children.add(treeModel);
        }
        return children;
    }

    public List<SsMaterialInst> queryByTypeName(String typeName){
        SsMaterialInst ssMaterialInst=new SsMaterialInst();
        QueryWrapper<SsMaterialInst> queryWrapper = new QueryWrapper<SsMaterialInst>();
        ssMaterialInst.setDelFlag(CommonConstant.DEL_FLAG_0);
        ssMaterialInst.setTypeName(typeName);
        QueryWrapperUtil.initQueryWrapper(queryWrapper, ssMaterialInst);
        return this.list(queryWrapper);
    }
}
